
var filecount = 1;
var locationCount = 1;
var markerCount = 0;
var markers = new Array();
var map;
var xhr;//通过ajax得到某个分科科下所有属的变量
var xhr_kindke;
var xhr_kindshu;
var ke;
var shu;


/**
 * 提交表单前，先验证下输入内容的合法性
 * @return
 */
function validation()
{
	return true;
}
/**
 * 增加分类
 * @param kind
 * @return
 */
function addkind(kind)
{
	if(kind == 1)//增加科分类
	{
		var kind = window.prompt('请输入新的科分类：');
		if(kind == null || kind == ''){
			return;
		}

		ke = kind;
		var json_url = "json/plantManager-addKindKe.html?kindKe="+kind;
		xhr_kindke = null;
		xhr_kindke = connectXHR(json_url, addKindKe);
	}
	else//增加属分类
	{
		var kindKe = document.getElementById("ke").value;
		if(kindKe==null || kindKe==""){
			$( "#dialogkefirst" ).dialog( "open" );
			return;
		}
		var kind = window.prompt('请输入新的属分类：');
		if(kind == null || kind == ''){
			return;
		}
		shu = kind;
		var json_url = "json/plantManager-addKindShu.html?kindKe="+kindKe+"&kindshu="+kind;
		xhr_kindshu = null;
		xhr_kindshu = connectXHR(json_url, addKindShu);
	}
}

function addKindKe(){
	if(xhr_kindke == null)return;
	if(xhr_kindke.readyState == 4 && xhr_kindke.status == 200) {
		var data = eval('(' + xhr_kindke.responseText + ')');
		var isExit = data.isKindKeExit;
		var idkind = data.kindKe;
		var kelist = document.getElementById("ke");
		if(isExit == "false"){
			$( "#dialogkeexist" ).dialog( "open" );
		}else{
			var ops = document.createElement("option");
			ops.setAttribute("value", idkind);
			ops.appendChild(document.createTextNode(ke));
			kelist.appendChild(ops);
			kelist.value=idkind;
		}
	}
}
function addKindShu(){
	if(xhr_kindshu == null)return;
	if(xhr_kindshu.readyState == 4 && xhr_kindshu.status == 200) {
		var data = eval('(' + xhr_kindshu.responseText + ')');
		var isExit = data.isKindShuExit;
		var idkind = data.kindshu;
		var shulist = document.getElementById("shu");
		if(isExit == "false"){
			$( "#dialogshuexist" ).dialog( "open" );
		}else{
			var ops = document.createElement("option");
			ops.setAttribute("value", idkind);
			ops.appendChild(document.createTextNode(shu));
			shulist.appendChild(ops);
			shulist.value=idkind;
		}
	}
}
/**
 * 增加一个上传图片的上传控件
 * 
 * @return
 */
function addphoto()
{
	filecount++;
	//var photohtml = '<br/><br/><div class="text"></div><input type="file" value="上传" size="20" name="upload"/>';
	var photohtml = '<br/><br/><div class="text"></div><input type="file" class="file" accept="image/*" id="f'
				+ filecount + '" name="upload" onchange="t' + filecount
				+ '.value=this.value"/><input type="text" id="t' + filecount
				+ '" readonly="readonly"></input><input type="button" value="上传" class="addkind"'
				+ ' onclick="f' + filecount + '.click()"/>';
	var html = document.createElement("div");
	html.innerHTML = photohtml;
	
	document.getElementById("photos").appendChild(html);
}
/**
 * 删除一个上传图片的上传控件
 * @return
 */
function removephoto()
{
	var photo = document.getElementById("photos");
	var length = 0;
	for(var i = 0; i<photo.childNodes.length;i++)
	{
		if(photo.childNodes[i].nodeType == 1)
			length++;
	}
	if(length == 1)
		return;
	photo.removeChild(getLastchild(photo));
}
/**
 * 得到一个结点的最后一个子结点
 * @param photo
 * @return
 */
function getLastchild(photo)
{
	var node = photo.lastChild;
	while(node.nodeType != 1)
	{
		node = node.previousSibling;
	}
	return node;
}
/**
 * 得到分类科下的所有分类属
 * @param kindshu
 * @return
 */
function getKindKe(kindke)
{
	if(kindke == null || kindke == "")
	{
		kindke = 0;
	}
	var kindShulist = document.getElementById("shu");
	 //把原来的分类科下拉框删掉
	for (var i = kindShulist.childNodes.length; i > 2; i--){
		kindShulist.removeChild(kindShulist.lastChild);
	}
	
	var json_url = "json/plantManager-listKindShuByKindKe.html?kindKe="+kindke;
	xhr = null;
	xhr = connectXHR(json_url, listKindShu);
}
/**
 * 填充分类属选择框
 * @return
 */
function listKindShu()
{
	if(xhr == null)return;
	if(xhr.readyState == 4 && xhr.status == 200) {
		var data = eval('(' + xhr.responseText + ')');
		var kindshu = data;
		if(kindshu == null || kindshu.length == 0)
			return;
		var kindShulist = document.getElementById("shu");
		for(var i = 0; i < kindshu.length; i++)
		{
			var ops = document.createElement("option");
			ops.setAttribute("value", kindshu[i].value);
			ops.appendChild(document.createTextNode(kindshu[i].kindShuName));
			kindShulist.appendChild(ops);
		}
	}
}
function initMap()
{
	var latlng = new google.maps.LatLng(24.437736364171066,118.09754759073257);
	map = new google.maps.Map(document.getElementById('map'), {  
	    zoom: 14,  
	    center: latlng,  
	    mapTypeId: google.maps.MapTypeId.ROADMAP  
	});
	google.maps.event.addListener(map, 'click', function(event) {
	    placeMarker(event.latLng);
	});
	var sw =  new google.maps.LatLng(24.429128461693962,118.07772142648697);
	var ne =  new google.maps.LatLng(24.45523099834168,118.1123082947731);
	//自定义地图的显示范围:厦大周边  
	var strictBounds = new google.maps.LatLngBounds(  
	    sw, ne);  
	google.maps.event.addListener(map, 'dragend',  
	function() {  
	    if (strictBounds.contains(map.getCenter())) return;  
	    var c = map.getCenter(),  
	    x = c.lng(),  
	    y = c.lat(),  
	    maxX = strictBounds.getNorthEast().lng(),  
	    maxY = strictBounds.getNorthEast().lat(),  
	    minX = strictBounds.getSouthWest().lng(),  
	    minY = strictBounds.getSouthWest().lat();  
	    if (x < minX) x = minX;  
	    if (x > maxX) x = maxX;  
	    if (y < minY) y = minY;  
	    if (y > maxY) y = maxY;  
	    map.setCenter(new google.maps.LatLng(y, x));  
	});
}
/**
 * 放置一个图标在地图上
 * @param location
 * @return
 */
function placeMarker(location) {

	if (markers[markerCount] != null) {// 第一次点击地图
		markers[markerCount].setMap(null);
	}

	var marker = new google.maps.Marker( {
		position : location,
		map : map
	});
	markers[markerCount] = marker;

	map.setCenter(location);
	var lngtext = document.getElementById("lng" + locationCount);
	lngtext.value = location.lng();
	var lattext = document.getElementById("lat" + locationCount);
	lattext.value = location.lat();
}
/**
 * 增加一个地理位置的经纬度输入框
 * 
 * @return
 */
function addlocation() {
	locationCount++;
	var photohtml = '<br/><br/><div class="text"><label>经度</label></div><input type="text" class="longitude" id="lng'
			+ locationCount
			+ '" name="plant.longitude" readonly="readonly"/>'
			+ '<br /><br /><div class="text"><label>纬度</label></div><input type="text" class="latitude" id="lat'
			+ locationCount
			+ '" name="plant.latitude" readonly="readonly"/>';
	var html = document.createElement("div");
	html.innerHTML = photohtml;

	document.getElementById("location").appendChild(html);
	
	markerCount++;
}
/**
 * 删除最后一个经纬度输入框
 * @return
 */
function removelocation(){
	var photo = document.getElementById("location");
	var length = 0;
	for(var i = 0; i<photo.childNodes.length;i++)
	{
		if(photo.childNodes[i].nodeType == 1)
			length++;
	}
	if(length == 1)
		return;
	photo.removeChild(getLastchild(photo));
	locationCount--;
	if(markers[markerCount] != null){
		markers[markerCount].setMap(null);
	}
	markerCount--;
}
/**
 * 修改界面时，科的初始化
 * @return
 */
function initKindke(kindke){
	var kelist = document.getElementById("ke");
	kelist.value = kindke;
}
/**
 * 修改界面时，属的初始化
 * @return
 */
function initKindshu(kindshu){
	var shulist = document.getElementById("shu");
	shulist.value = kindshu;
}
/**
 * 修改界面时，专题的初始化
 * @return
 */
function initSubject(sub){
	var subjectlist = document.getElementById("subject");
	subjectlist.value = sub;
}